"""
Case Type   : WDR
Case Name   : 开启数据库监控快照功能，不同库中查看快照数据
Create At   : 2023.1.5
Owner       : @haomeng802
Description :
    1、修改参数，开启快照功能
    2、使用默认系统库，查看当前快照数据
    3、使用非默认系统库，查看当前快照数据
    4、清理环境，恢复参数
Expect      :
    1、开启快照功能成功
    2、查看快照数据成功
    3、查看快照数据失败，非系统库不可查看
    4、清理环境成功，恢复参数成功
History     :
    modified by @haomeng802 at 2023/1/31:优化用例
"""

import os
import time
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class WDR(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.sh_pri = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.config_wdr = "enable_wdr_snapshot"
        self.table = "snapshot.snapshot"

    def test_wdr(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '-----step1:检查参数，修改参数配置;expect:成功-----'
        self.log.info(text)

        check_res = self.sh_pri.execut_db_sql(f'''show {self.config_wdr};''')
        self.log.info(check_res)
        if 'on' != check_res.splitlines()[-2].strip():
            self.sh_pri.execute_gsguc('reload',
                                      self.constant.GSGUC_SUCCESS_MSG,
                                      f'''{self.config_wdr} = "on"''')

        param = self.sh_pri.execut_db_sql(f'''show {self.config_wdr};''')
        self.log.info(param)
        self.assertEqual('on', param.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '-----step2:使用系统默认库postgres查看快照数据;expect:查看快照数据成功-----'
        self.log.info(text)

        time.sleep(10)
        select_cmd = f'''select * from {self.table};'''
        show_res1 = self.sh_pri.execut_db_sql(select_cmd, dbname='postgres')
        self.log.info(show_res1)
        self.assertEqual('snapshot_id',
                         show_res1.splitlines()[0].split('|')[0].strip(),
                         '执行失败:' + text)

        text = '-----step3:使用非系统默认库查看快照数据;expect:查看快照数据失败-----'
        self.log.info(text)

        show_res2 = self.sh_pri.execut_db_sql(select_cmd)
        self.log.info(show_res2)
        self.assertIn('ERROR', show_res2, '执行失败:' + text)

    def tearDown(self):
        text = '-----step3:清理环境,还原参数;expect:成功-----'
        self.log.info(text)

        drop_snapshot = f'''truncate table {self.table};
            select count(*) from {self.table};'''
        self.log.info(drop_snapshot)
        drop_res = self.sh_pri.execut_db_sql(drop_snapshot, dbname='postgres')
        self.log.info(drop_res)

        recov_res = self.sh_pri.execut_db_sql(f'''show {self.config_wdr};''')
        self.log.info(recov_res)
        if 'off' != recov_res.splitlines()[-2].strip():
            self.sh_pri.execute_gsguc('reload',
                                      self.constant.GSGUC_SUCCESS_MSG,
                                      f'''{self.config_wdr} = "off"''')

        param = self.sh_pri.execut_db_sql(f'''show {self.config_wdr};''')
        self.log.info(param)

        self.assertEqual('off', param.splitlines()[-2].strip(), '参数修改成功')
        self.assertEqual('0', drop_res.splitlines()[-2].strip(),
                         '执行失败:' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
